export default {
    data() {
        return {
            currentReasonId: null,
            selectedGoods: null,
            orderId: null,
            order: {},
            priceApply: 0,
            deviceId: null,
            goodsList: [],
            reasonsList: [{
                    id: "0",
                    value: "我没有购买这个商品或购买的是其它商品"
                },
                {
                    id: "1",
                    value: "实际购数量与订单显示不符"
                },
                {
                    id: "2",
                    value: "其他"
                }
            ]
        }
    },
    onLoad(options) {
        this.orderId = options.orderId
        this.getOrder()
    },
    methods: {
        getOrder: function() {
            const _this = this
            let data = {
                orderId: this.orderId, // 空值查全部，非零查准确订单
            }
            this.$request(this.$api.GetOrder, data, "GET").then(res => {
                if (res.code == 200) {
                    // res.result = res.result || [];
                    _this.order = res.result
                    _this.goodsList = _this.order.orderDetailList
                    _this.deviceId = res.result.deviceId
                } else {
                    uni.showToast({
                        title: '获取订单错误',
                        icon: "none"
                    });
                }
            })
        },
        checkboxChange: function(e) {
            const values = e.detail.value;
            this.selectedGoods = values
            console.log(this.selectedGoods)
        },
        radioChange: function(evt) {
            this.currentReasonId = evt.target.value
        },
        submitData() {
            if (!this.currentReasonId || !this.selectedGoods || this.selectedGoods.length === 0) {
                uni.showToast({
                    title: '请勾选信息',
                    duration: 2000,
                    icon: 'none'
                })
                return
            } else {
                console.log(parseInt(this.priceApply))
                if (this.priceApply <= 0) {
                    uni.showToast({
                        title: '请输入退款金额',
                        duration: 2000,
                        icon: 'none'
                    })
                    return false
                }
                let refundGoodList = []
                let maxAmount = 0
                const _this = this
                for (let i in _this.goodsList) {
                    if (_this.selectedGoods.indexOf(_this.goodsList[i]['goodsId']) != -1) {
                        // refundGoodList.push(_this.goodsList[i])
                        maxAmount += _this.goodsList[i].saleCountPrice * 100
                    }
                }
                maxAmount = maxAmount / 100
                if (maxAmount < this.priceApply) {
                    uni.showToast({
                        title: '提交金额不能超过最大金额：' + maxAmount,
                        duration: 2000,
                        icon: 'none'
                    })
                    return false
                }
                let data = {
                    "deviceId": this.deviceId,
                    "orderId": this.orderId,
                    "priceApply": this.priceApply,
                    "refundGoodsIdList": _this.selectedGoods,
                    "refundRemark": this.reasonsList[this.currentReasonId].value,
                    "shopId": this.order.administratorId,
                }
                this.$request(this.$api.applyRefund, data, "post").then(res => {
                    if (res.code == 200) {
                        uni.showToast({
                            title: '售后申请已提交,运营商审核后资金会原路退回',
                            duration: 2000,
                            icon: 'none'
                        })
                    } else {

                        if (res.code == 600) {
                            uni.showToast({
                                title: res.message,
                                icon: "none"
                            });
                        } else {
                            uni.showToast({
                                title: '提交失败',
                                icon: "none"
                            });
                        }


                    }
                })

            }


        }
    }
}